using System;
using UnityEngine;

namespace NodeCanvas.Variables
{
	[Obsolete]
	[AddComponentMenu("")]
	public class ComponentData : VariableData
	{
		[Serializable]
		private class SerializedComponent
		{
			public string path;

			public Type trueType;

			public SerializedComponent(string path, Type type)
			{
				this.path = path;
				trueType = type;
			}
		}

		public Component value;

		[SerializeField]
		private string _typeName = typeof(Component).AssemblyQualifiedName;

		private Type type
		{
			get
			{
				return (!(value != null)) ? Type.GetType(_typeName) : value.GetType();
			}
			set
			{
				_typeName = value.AssemblyQualifiedName;
				if (this.value != null && !value.NCIsAssignableFrom(this.value.GetType()))
				{
					this.value = null;
				}
			}
		}

		public override Type varType
		{
			get
			{
				return type;
			}
		}

		public override object objectValue
		{
			get
			{
				return value;
			}
			set
			{
				this.value = (Component)value;
				if (value != null && !type.NCIsAssignableFrom(value.GetType()))
				{
					type = value.GetType();
				}
			}
		}

		public override object GetSerialized()
		{
			if (value == null)
			{
				return new SerializedComponent(null, type);
			}
			GameObject gameObject = value.gameObject;
			string text = "/" + gameObject.name;
			while (gameObject.transform.parent != null)
			{
				gameObject = gameObject.transform.parent.gameObject;
				text = "/" + gameObject.name + text;
			}
			return new SerializedComponent(text, value.GetType());
		}

		public override void SetSerialized(object obj)
		{
			SerializedComponent serializedComponent = obj as SerializedComponent;
			if (serializedComponent == null)
			{
				value = null;
				type = typeof(Component);
				return;
			}
			type = serializedComponent.trueType;
			if (string.IsNullOrEmpty(serializedComponent.path))
			{
				return;
			}
			GameObject gameObject = GameObject.Find(serializedComponent.path);
			if (!gameObject)
			{
				Debug.LogWarning("ComponentData Failed to load. The component's gameobject was not found in the scene. Path '" + serializedComponent.path + "'");
				return;
			}
			value = gameObject.GetComponent(serializedComponent.trueType);
			if (value == null)
			{
				Debug.LogWarning("ComponentData Failed to load. GameObject was found but the component of type '" + serializedComponent.trueType.ToString() + "' itself was not. Path '" + serializedComponent.path + "'");
			}
		}
	}
}
